<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.mapper.${mapperName}">


    <sql id="Base_Column_List">
        <trim prefix="" suffix="" suffixOverrides=",">
            <#list columns as column>
                ${abbreviationName}.`${column.columnName}`,
            </#list>
        </trim>
    </sql>
    <resultMap id="BaseResultMap" type="${packageName}.pojo.${modelName}">
        <#if !plugin.enableMybatisplus>
            <#list columns as column>
                <<#if column.isPrimary=="true" >id<#else>result</#if> column="${column.columnName}" property="${column.propertyName?uncap_first}" jdbcType="${column.type}"/>
            </#list>
        </#if>

    </resultMap>
    <#if !plugin.enableMybatisplus>

        <!--增加数据 insert-->
        <insert id="insert${modelName}" parameterType="${packageName}.pojo.${modelName}" <#if primaryKeyColumns ?has_content  && (primaryKeyColumns?size > 0) && (primaryKeyColumns?size < 2)> <#if primaryKeyColumns[0].isAutoincrement='YES'> keyProperty="${primaryKeyColumns[0].propertyName?uncap_first}" useGeneratedKeys="true" </#if> </#if>>
            insert into ${tableName}
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <#list columns as column>
                    <if test="${column.propertyName?uncap_first} != null">
                        `${column.columnName}`,
                    </if>
                </#list>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <#list columns as column>
                    <if test="${column.propertyName?uncap_first} != null">
                        ${r"#{"}${column.propertyName?uncap_first},jdbcType=${column.type}},
                    </if>
                </#list>
            </trim>
        </insert>
        <#if primaryKeyColumns ?has_content  && (primaryKeyColumns?size > 0)>
            <!--删除数据 deleteByPrimaryKey-->
            <delete id="delete${modelName}ByPrimaryKey" parameterType="${packageName}.pojo.${modelName}">
                delete from ${tableName}
                where
                <#list primaryKeyColumns as column>

                    <#if column_index != 0> and </#if>`${column.columnName}` = ${r"#{"}${column.propertyName?uncap_first},jdbcType=${column.type}}

                </#list>
            </delete>
        </#if>

        <#if primaryKeyColumns ?has_content  && (primaryKeyColumns?size > 0)>
            <!--批量删除数据 -->
            <delete id="delete${modelName}ByIds" parameterType="java.lang.String">
                delete
                from ${tableName}
                where
                <#list primaryKeyColumns as column>

                    <#if column_index != 0> and </#if>`${column.columnName}` in ${r"(${"}${column.propertyName?uncap_first}s})

                </#list>
            </delete>
        </#if>

        <#if primaryKeyColumns ?has_content  && (primaryKeyColumns?size > 0)>
            <!--修改数据 updateByPrimaryKey-->
            <update id="update${modelName}ByPrimaryKey" parameterType="${packageName}.pojo.${modelName}">
                update ${tableName}
                <set>
                    <#list columns as column>
                        <#if column.isPrimary=="false">
                            `${column.columnName}` = ${r"#{"}${column.propertyName?uncap_first},jdbcType=${column.type}},
                        </#if>
                    </#list>
                </set>
                where
                <#list primaryKeyColumns as column>

                    <#if column_index != 0> and </#if>`${column.columnName}` = ${r"#{"}${column.propertyName?uncap_first},jdbcType=${column.type}}

                </#list>
            </update>
        </#if>

        <#if primaryKeyColumns ?has_content  && (primaryKeyColumns?size > 0)>
            <!--修改属性不为空数据 updateByPrimaryKey-->
            <update id="update${modelName}ByPrimaryKeyNotNull" parameterType="${packageName}.pojo.${modelName}">
                update ${tableName}
                <set>
                    <#list columns as column>
                        <#if column.isPrimary=="false">
                            <if test="${column.propertyName?uncap_first} != null">
                                `${column.columnName}` = ${r"#{"}${column.propertyName?uncap_first},jdbcType=${column.type}},
                            </if>
                        </#if>
                    </#list>
                </set>
                where
                <#list primaryKeyColumns as column>

                    <#if column_index != 0> and </#if>`${column.columnName}`
                    = ${r"#{"}${column.propertyName?uncap_first},jdbcType=${column.type}}

                </#list>
            </update>
        </#if>
        <select id="getAll${modelName}s" resultMap="BaseResultMap">
            select     <include refid="Base_Column_List"/>
            from ${tableName}  as ${abbreviationName};
        </select>

        <#if primaryKeyColumns ?has_content  && (primaryKeyColumns?size > 0)>
            <select id="select${modelName}ByPrimaryKey" resultMap="BaseResultMap" >
                select
                <include refid="Base_Column_List"/>
                from ${tableName}  as ${abbreviationName}
                where
                <#list primaryKeyColumns as column>

                    <#if column_index != 0> and </#if> ${abbreviationName}.`${column.columnName}` = ${r"#{"}${column.propertyName?uncap_first},jdbcType=${column.type}}

                </#list>
            </select>
        </#if>
    </#if>

    <sql id="conditions">
        <if test="parameter.customConditionsSql !=null  and parameter.customConditionsSql != '' ">
            and (${r"${"}parameter.customConditionsSql})

        </if>
        <if test="parameter.likeNameSearch !=null">

            <foreach collection="parameter.likeNameSearch" item="value" index="key">

                <if test="null != value">
                    and  ${abbreviationName}.`${r"${"}key}` like CONCAT('%',
                    ${r"#{"}value},'%')
                </if>
            </foreach>
        </if>
        <if test="parameter.multipleChoiceCondition !=null">
            <foreach collection="parameter.multipleChoiceCondition.entrySet()" item="value" index="key">
                and  ${abbreviationName}.`${r"${"}key}` in
                <foreach collection="value" item="list" index="index" open="(" close=")" separator=",">
                    ${r"#{"}list}
                </foreach>
            </foreach>
        </if>

        <if test="parameter.fieldConditionList !=null">
            <foreach collection="parameter.fieldConditionList" item="item" index="index">
                <if test="item.relationId == 'equal'">
                    and  ${abbreviationName}.`${r"${"}item.databaseId}` =  ${r"#{"}item.value}
                </if>
                <if test="item.relationId == 'not-equal'">
                    and  ${abbreviationName}.`${r"${"}item.databaseId}` !=  ${r"#{"}item.value}
                </if>

            </foreach>

        </if>
        <if test="parameter.universalTime !=null ">
            <foreach collection="parameter.universalTime.entrySet()" item="value"  index="key">

                <if test="value.startTime !=null and value.startTime !='' ">

                    <choose>
                        <when test="value.includeStartValue !=null and value.includeStartValue ==false">
                            and  ${abbreviationName}.${r"${"}key} <![CDATA[ > ]]> ${r"#{"}value.startTime}
                        </when>
                        <otherwise>
                            and  ${abbreviationName}.${r"${"}key} <![CDATA[ >= ]]> ${r"#{"}value.startTime}
                        </otherwise>
                    </choose>


                </if>

                <if test="value.endTime !=null and value.endTime !='' ">

                    <choose>
                        <when test="value.includeEndValue !=null and value.includeEndValue ==false">
                            and  ${abbreviationName}.${r"${"}key} <![CDATA[ < ]]> ${r"#{"}value.endTime}
                        </when>
                        <otherwise>
                            and ${abbreviationName}.${r"${"}key} <![CDATA[ <= ]]> ${r"#{"}value.endTime}
                        </otherwise>
                    </choose>


                </if>



            </foreach>
        </if>
            <if test="parameter.isNullAttributes !=null ">
            <foreach collection="parameter.isNullAttributes" index="index"
                     item="item">
                AND ( ${abbreviationName}.`${r"${"}item}` is null or  ${abbreviationName}.${r"${"}item} = '' )
            </foreach>

        </if>
        <if test="parameter.notNullAttributes !=null ">

            <foreach collection="parameter.notNullAttributes" index="index"
                     item="item">
                AND ( ${abbreviationName}.`${r"${"}item}` is not null and  ${abbreviationName}.${r"${"}item} != '')
            </foreach>

        </if>
        <if test="parameter.data !=null">

            <#list columns as column>
                <if test="parameter.data.${column.propertyName?uncap_first} != null  <#if column.type='VARCHAR'||column.type='TEXT'||column.type='CHAR'>  and parameter.data.${column.propertyName?uncap_first} != ''  </#if>">
                    and  ${abbreviationName}.`${column.columnName}` = ${r"#{parameter.data."}${column.propertyName?uncap_first},jdbcType=${column.type}}
                </if>
            </#list>

        </if>
    </sql>
    <sql id="isNull">
        <if test="parameter.data !=null">

            <#list columns as column>
                <if test="parameter.data.${column.propertyName?uncap_first} == null  ">
                    and  ${abbreviationName}.`${column.columnName}` is null
                </if>
            </#list>

        </if>
    </sql>
    <select id="select${modelName}" resultMap="BaseResultMap" parameterType="${packageName}.result.Parameter">
        select
        <include refid="Base_Column_List"/>
        from ${tableName} as  ${abbreviationName}
        <where>
            <include refid="conditions"></include>
        </where>
    </select>

    <!--总条数-->
    <select id="get${modelName}PageCount" resultType="long" parameterType="${packageName}.result.Parameter">
        select
      count(1)
        from ${tableName} as ${abbreviationName}
        <where>
            <include refid="conditions"></include>
        </where>
    </select>

    <!--分页查询-->
    <select id="get${modelName}Page" resultMap="BaseResultMap"
            parameterType="${packageName}.result.Parameter">
        select
        <include refid="Base_Column_List"/>
        from ${tableName} as  ${abbreviationName}
        <where>
            <include refid="conditions"></include>
        </where>

        <choose>
            <when test="parameter.orderBy != null and parameter.orderBy!= ''">
                order by ${abbreviationName}.${r"${"}parameter.orderBy}
            </when>
            <!-- 没有激活排序条件时，默认按商品发布时间倒序排序 -->
            <otherwise>
                order by <#list primaryKeyColumns as column>

                    <#if column_index == 0> ${abbreviationName}.`${column.columnName}` </#if>

                </#list>
            </otherwise>
        </choose>
        <if test="parameter.desc!= null and parameter.desc!= '' and parameter.desc=='true'">
            DESC
        </if>
        <if test="parameter.isLimit ==null or  parameter.isLimit == '' or parameter.isLimit != 'false'">


            <if test="parameter.pageSize !=null and parameter.pageSize>0">


                LIMIT ${r"#{"}parameter.startIndex},${r"#{"}parameter.pageSize}
            </if>
        </if>
    </select>







</mapper>